package com.ywq.app.dao.impl;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.mybatis.spring.support.SqlSessionDaoSupport;

import com.ywq.app.dao.AddressCodeDao;
import com.ywq.app.domain.AddressCode;
import com.ywq.common.exception.AppBusinessException;




public class AddressCodeDaoImpl extends SqlSessionDaoSupport implements
		AddressCodeDao {

	private static Logger logger = LoggerFactory
			.getLogger(AddressCodeDaoImpl.class);

	// 插入地名
	@Override
	public void addAddressCode(AddressCode addressCode) throws AppBusinessException {
		try {
			getSqlSession()
					.insert("com.ywq.app.persistence.AddressCodeMapper.insertAddressCode",
							addressCode);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("insertAddressCode database failure.", e);
			}
			throw new AppBusinessException("insertAddressCode database failure.",
					e);
		}
	}

	// 取得全部的省/直辖市的名称
	@SuppressWarnings("unchecked")
	@Override
	public List<AddressCode> getProvinceList() throws AppBusinessException {
		try {
			return getSqlSession()
					.selectList(
							"com.ywq.app.persistence.AddressCodeMapper.getProvinceList");
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getProvinceList database failure.", e);
			}
			throw new AppBusinessException("getProvinceList database failure.",
					e);
		}
	}

	/**
	 * author Bruce Zhang
	 * @param province
	 * @return			根据省/直辖市取得市/区
	 * @throws AppBusinessException
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<AddressCode> getCityByProvince(String province)
			throws AppBusinessException {
		try {
			return getSqlSession()
					.selectList(
							"com.ywq.app.persistence.AddressCodeMapper.getCityByProvince",
							province);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getCityByProvince database failure.", e);
			}
			throw new AppBusinessException("getCityByProvince database failure.",
					e);
		}
	}

	/**
	 * author Bruce Zhang
	 * @param city		城市/直辖市的区
	 * @return			根据市/区取得县/街道
	 * @throws AppBusinessException
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<AddressCode> getCountyByCity(String city)
			throws AppBusinessException {
		try {
			return getSqlSession()
					.selectList(
							"com.ywq.app.persistence.AddressCodeMapper.getCountyByCity",
							city);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getCountyByCity database failure.", e);
			}
			throw new AppBusinessException("getCountyByCity database failure.",
					e);
		}
	}

	/**
	 * author Bruce Zhang
	 * @param county	县
	 * @return			根据县取得镇/街道
	 * @throws AppBusinessException
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<AddressCode> getTownByCounty(String county)
			throws AppBusinessException {
		try {
			return getSqlSession()
					.selectList(
							"com.ywq.app.persistence.AddressCodeMapper.getTownByCounty",
							county);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getTownByCounty database failure.", e);
			}
			throw new AppBusinessException("getTownByCounty database failure.",
					e);
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<AddressCode> getAddress(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return getSqlSession()
					.selectList(
							"com.ywq.app.persistence.AddressCodeMapper.getAddress",
							map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getAddress database failure.", e);
			}
			throw new AppBusinessException("getAddress database failure.",
					e);
		}
	}

}
